package cn.usian.basicInformation.car.controller;

import cn.usian.basicInformation.car.service.ICarService;
import cn.usian.basicInformation.log.commons.Constants;
import cn.usian.basicInformation.log.commons.Log;
import cn.usian.dto.basicInformation.car.BaseCarTypeManageDto;
import cn.usian.pojo.Result;
import cn.usian.pojo.basicInformation.car.BaseCarTypeManage;
import cn.usian.vo.basicInformation.car.BaseCarTypeManageVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/car")
@Slf4j
public class CarController {
    @Autowired
    private ICarService carService;

    @GetMapping("/list")
    public Result getCarList(BaseCarTypeManageDto carTypeManageDto){
        log.info("carTypeManageDto:{}",carTypeManageDto);
        PageInfo<BaseCarTypeManageVo> carList = carService.list(carTypeManageDto);
        return Result.ok(carList);
    }

    @PostMapping("/findById/{id}")
    public Result getCarId(@PathVariable long id){
        BaseCarTypeManage car = carService.findById(id);
        return Result.ok(car);
    }

    @PostMapping("/save")
    public Result carSave(@RequestBody BaseCarTypeManage carTypeManage){
        Result save = carService.save(carTypeManage);
        log.info("save:{}",save);
        if(save.getCode() == 200){
            return Result.ok();
        }else{
            return Result.error(save.getMsg());
        }
    }

    @Log(operationModule = "车辆类型配置", operationType = Constants.INSERT)
    @PostMapping("/insert")
    public Result insert(@RequestBody BaseCarTypeManage carTypeManage){
        Result insert = carService.save(carTypeManage);
        log.info("insert:{}",insert);
        if(insert.getCode() == 200){
            return Result.ok();
        }else{
            return Result.error(insert.getMsg());
        }
    }

    @Log(operationModule = "车辆类型配置", operationType = Constants.UPDATE)
    @PostMapping("/update")
    public Result carUpdate(@RequestBody BaseCarTypeManage carTypeManage){
        Result update = carService.save(carTypeManage);
        log.info("update:{}",update);
        if(update.getCode() == 200){
            return Result.ok();
        }else{
            return Result.error(update.getMsg());
        }
    }



}
